package group32.client;
import java.util.ArrayList;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;

public class Friend extends Composite {
	int userid=-1;
	private FriendServiceAsync pService = GWT
	.create(FriendService.class);
public Friend (final int userID, final String username){
	    final VerticalPanel finalpanel= new VerticalPanel();
	    initWidget(finalpanel);
	    final Label lbl2 = new Label("Friends Recommendations");	
	    final DialogBox dialogBox = new DialogBox();
  		final HTML serverResponseLabel = new HTML();
  		final Button closeButton = new Button("Close");
  	
  		closeButton.getElement().setId("closeButton");
 
  		pService.getRelation(userID,new AsyncCallback<ArrayList<String>>() {

  			@Override
  			public void onFailure(Throwable caught) {
  				// Show the RPC error message to the user
  				dialogBox
  						.setText("Remote Procedure Call - Failure");
  				serverResponseLabel
  						.addStyleName("serverResponseLabelError");
  				serverResponseLabel.setHTML("SERVER_ERROR");
  				dialogBox.show();
  				dialogBox.center();
  				
  				closeButton.setFocus(true);
  			}

  			@Override
  			public void onSuccess(ArrayList<String> result) {
  				final VerticalPanel Panel= new VerticalPanel();
  				Panel.setSpacing(10);
				final VerticalPanel HPanel= new VerticalPanel();
				HPanel.setSpacing(20);
				final VerticalPanel H2Panel= new VerticalPanel();
				H2Panel.setSpacing(10);
				final HorizontalPanel FPanel= new HorizontalPanel();
				FPanel.setSpacing(10);
				Panel.add(lbl2);
				     final String fname1=result.get(0);
				     final String fname2=result.get(2);
					 final Label fullname1= new Label (fname1);
					 final Label fullname2= new Label (fname2);
					 final Button btn2=new Button("ADD");
					 final Button btn3=new Button("ADD");
					 HPanel.add(fullname1);
					 H2Panel.add(btn2);
					 HPanel.add(fullname2);
					 H2Panel.add(btn3);
					 FPanel.add(HPanel);
					 FPanel.add(H2Panel);
					final int fid1= Integer.parseInt(result.get(1));
					final int fid2= Integer.parseInt(result.get(3));
					
					 btn2.addClickHandler(new ClickHandler() { 
				  			
				  			public void onClick(ClickEvent event) { 
				  				final DialogBox dialogBox = new DialogBox();
				  				dialogBox.setVisible(true);
				  				dialogBox.setText("Select Circle");
				  				dialogBox.setAnimationEnabled(true);
				  				final Button closeButton = new Button("Close");
				  				// We can set the id of a widget by accessing its Element
				  				closeButton.getElement().setId("closeButton");
//				  				final HTML schemaLabel = new HTML();
//				  				final HTML serverResponseLabel = new HTML();
				  				VerticalPanel dialogVPanel = new VerticalPanel();
				  				dialogVPanel.addStyleName("dialogVPanel");
				  				//dialogVPanel.add(new HTML("<br><b>Circle name:</b>"));
				  				//dialogVPanel.add(schemaLabel);
				  				final TextBox circlename= new TextBox();
				  				//dialogVPanel.add(new HTML("<br><b>Returned data:</b>"));
				  				dialogVPanel.setHorizontalAlignment(VerticalPanel.ALIGN_RIGHT);
				  				dialogBox.setWidget(dialogVPanel);
				  				final Button addcir = new Button("ok");
				  				dialogVPanel.add(circlename);
				  				dialogVPanel.add(addcir);
				  				dialogVPanel.add(closeButton);
				  				dialogBox.show();
								dialogBox.center();
								final String textval=circlename.getText();
								
								closeButton.setFocus(true);
								addcir.addClickHandler(new ClickHandler() {
				  					
				  					@Override
				  					public void onClick(ClickEvent event) {
				  						if (circlename.getText().isEmpty()) {
				  							Window.alert("Sorry, you must enter a circle to add a friend");
				  						} else 
				  						if ( (circlename.getText().contains("family")))
				  						{
				  						callInsertFriend(userID,fname1, fid1);
										fullname1.setVisible(false);
										btn2.setVisible(false);
										dialogBox.hide();
				  						}
				  						else 
					  						if ( (circlename.getText().contains("friends")))
					  						{
					  						callInsertFriend(userID,fname1, fid1);
											fullname1.setVisible(false);
											btn2.setVisible(false); 
											dialogBox.hide();
											
											
											
											
					  						}
					  						else
					  							Window.alert("Invalid circle. Select from friends and family");
				  					}});
								closeButton.addClickHandler(new ClickHandler() {

									@Override
									public void onClick(ClickEvent event) {
										dialogBox.hide();
									}
									
								});
								
								
							}
					  	   });
					 
					 btn3.addClickHandler(new ClickHandler() { 
				  			
						 public void onClick(ClickEvent event) { 
				  				final DialogBox dialogBox = new DialogBox();
				  				dialogBox.setVisible(true);
				  				dialogBox.setText("Select Circle");
				  				dialogBox.setAnimationEnabled(true);
				  				final Button closeButton = new Button("Close");
				  				// We can set the id of a widget by accessing its Element
				  				closeButton.getElement().setId("closeButton");
//				  				final HTML schemaLabel = new HTML();
//				  				final HTML serverResponseLabel = new HTML();
				  				VerticalPanel dialogVPanel = new VerticalPanel();
				  				dialogVPanel.addStyleName("dialogVPanel");
				  				//dialogVPanel.add(new HTML("<br><b>Circle name:</b>"));
				  				//dialogVPanel.add(schemaLabel);
				  				final TextBox circlename= new TextBox();
				  				//dialogVPanel.add(new HTML("<br><b>Returned data:</b>"));
				  				dialogVPanel.setHorizontalAlignment(VerticalPanel.ALIGN_RIGHT);
				  				dialogBox.setWidget(dialogVPanel);
				  				final Button addcir = new Button("ok");
				  				dialogVPanel.add(circlename);
				  				dialogVPanel.add(addcir);
				  				dialogVPanel.add(closeButton);
				  				dialogBox.show();
								dialogBox.center();
								final String textval=circlename.getText();
								
								closeButton.setFocus(true);
								addcir.addClickHandler(new ClickHandler() {
				  					
				  					@Override
				  					public void onClick(ClickEvent event) {
				  						if (circlename.getText().isEmpty()) {
				  							Window.alert("Sorry, you must enter a circle to add");
				  						} else 
				  						if ( (circlename.getText().contains("family")))
				  						{
				  						callInsertFriend(userID,fname2, fid2);
				  						fullname2.setVisible(false);
										btn3.setVisible(false); 
										dialogBox.hide();
				  						}
				  						else 
					  						if ( (circlename.getText().contains("friends")))
					  						{
					  						callInsertFriend(userID,fname2, fid2);
											fullname2.setVisible(false);
											btn3.setVisible(false); 
											dialogBox.hide();
											
											
											
											
					  						}
					  						else
					  							Window.alert("Invalid circle. Select from friends and family");
				  					}});
								closeButton.addClickHandler(new ClickHandler() {

									@Override
									public void onClick(ClickEvent event) {
										dialogBox.hide();
									}
									
								});
								
							}
					  	   });
  				
  finalpanel.add(Panel);
  finalpanel.add(FPanel);
	 }
   });}
  			private void callInsertFriend(int userID,String name, int newid2) {
				
				 final DialogBox dialogBox = new DialogBox();
			  	final HTML serverResponseLabel = new HTML();
			  	final Button closeButton = new Button("Close");
				pService.insertnewfriend(userID,name,newid2, new AsyncCallback<Void>() {
		  	          
		  	  			@Override
		  	  			public void onFailure(Throwable caught) {
		  	  				// Show the RPC error message to the user
		  	  				dialogBox
		  	  						.setText("Remote Procedure Call - Failure");
		  	  				serverResponseLabel
		  	  						.addStyleName("serverResponseLabelError");
		  	  				serverResponseLabel.setHTML("SERVER_ERROR");
		  	  				dialogBox.show();
		  	  				dialogBox.center();
		  	  				
		  	  				closeButton.setFocus(true);
		  	  			}

		  	  			@Override
		  	  			public void onSuccess(Void result) {
		  	  			Window.alert("Successfull Insertion");
		  	  			
		  	  			}			
		  			});//closing insertnewfriend
		
  }
}




